package mrusanov.ui.battle

import mrusanov.battle.PlayerState
import javax.swing.{SwingConstants, JLabel, JComponent, JPanel}
import java.awt.{Color, ComponentOrientation, Font, GridLayout}
import net.miginfocom.swing.MigLayout
import mrusanov.util.ColorGenerator

class UnitStatusViewComponent(miniArenaComponent:MiniArenaComponent, host:Boolean, topToBottom:Boolean) extends JPanel {
  private val VICTORY_FONT_SIZE = 30f
  setLayout(new GridLayout(0, 1))
  initComponents()


  def playerState:Option[PlayerState] = {
    if (host) {
      miniArenaComponent.host
    } else {
      miniArenaComponent.guest
    }
  }



  private def initComponents() {
    var children = playerState match {
      case Some(state) => componentsForPlayerState(state)
      case None => componentsForEmptyPlayerState
    }

    if (!topToBottom) {
      children = children.reverse
    }

    children foreach(add(_))
  }

  private def componentsForEmptyPlayerState:List[JComponent] = {
    val arena = miniArenaComponent.miniArena
    if (arena.isOver) {
      componentsForFinishedDuel
    } else if (arena.empty) {
       componentsForNotStartedDuel
    } else {
       componentsForCountdown
    }

  }

  private def componentsForFinishedDuel:List[JComponent] = {
    val currentTeam = host match {
      case true => miniArenaComponent.battle.hostTeam
      case false  => miniArenaComponent.battle.guestTeam
    }

    if (Some(currentTeam) == miniArenaComponent.miniArena.winningTeam) {
      componentsForVictory
    } else {
      componentsForDefeat
    }
  }

  private def componentsForVictory:List[JComponent] = {
    val label = new JLabel("V")
    label.setHorizontalTextPosition(SwingConstants.CENTER)
    label.setVerticalTextPosition(SwingConstants.BOTTOM)
    label.setFont(label.getFont.deriveFont(Font.BOLD, VICTORY_FONT_SIZE))
    List(label)
  }

  private def componentsForDefeat:List[JComponent] = List()

  private def componentsForNotStartedDuel:List[JComponent] = List()

  private def componentsForCountdown:List[JComponent] = {
    val label = new JLabel(miniArenaComponent.miniArena.countdownToWin.toString)
    label.setHorizontalTextPosition(SwingConstants.CENTER)
    label.setVerticalTextPosition(SwingConstants.BOTTOM)
    label.setFont(label.getFont.deriveFont(Font.BOLD, VICTORY_FONT_SIZE))
    List(label)
  }

  private def componentsForPlayerState(state:PlayerState):List[JComponent] = {
    val nameLabel = new JLabel(state.player.name)
    val healthLabel = new JLabel()
    healthLabel.setText(state.hp + "/" + state.player.hp)
    val attackLabel = new JLabel()
    attackLabel.setText(state.player.damage.toString)
    val defenceLabel = new JLabel("" + state.playerType.defence(miniArenaComponent.miniArena.terrain) + "%")

    val retList = List(nameLabel, healthLabel, attackLabel, defenceLabel)
    retList foreach (_.setHorizontalAlignment(SwingConstants.CENTER))
    retList
  }

  def refresh() {
    removeAll()
    initComponents()
    validate()
    repaint()
  }

}
